package net.rlair.sbdemo.controller;


import net.rlair.sbdemo.ContextUtils;
import net.rlair.sbdemo.dao.mybatis.bootmapper.UserMapper;
import net.rlair.sbdemo.entity.User;
import net.rlair.sbdemo.service.UserService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Date;

/**
 * @author lixudong
 */
@Controller
@RequestMapping("user")
public class PenghaohuanController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ContextUtils contextUtils;

    @Resource
    private UserService userService;


    @GetMapping("list")
    @ResponseBody
    public Object list() {
        return userMapper.selectUser();
    }
    

    @GetMapping("page/mybatis")
    @ResponseBody
    public Object pageMybatis(
            @RequestParam(value = "page", defaultValue = "0") final int page,
            @RequestParam(value = "pageSize", defaultValue = "15") final int pageSize) {
        return userService.userPageWithMybatis(PageRequest.of(page, pageSize));
    }

    @GetMapping("page/jpa")
    @ResponseBody
    public Object pageJpa(
            @RequestParam(value = "page", defaultValue = "0") final int page,
            @RequestParam(value = "pageSize", defaultValue = "15") final int pageSize) {
        return userService.userPageWithJpa(PageRequest.of(page, pageSize));
    }

    @RequestMapping("saveTwice")
    @ResponseBody
    public Object save(final User user) {
        user.setCreated(new Date());
        user.setUpdated(new Date());

        return (userService.saveWithJpa(user) + userService.saveWithMybatis(user)) == 2;
    }
}
